%{
                                                         getROIs.m V1
                                                     Written by Rajeev Kant
                                                        Sept. 4th, 2020
                                    Coulombe Lab for Cardiovascular Regenerative Engineering
                            Center for Biomedical Engineering, Brown Univeristy, Providence, RI, 02903

This function will read in a defined RGB region image (from applyRegion.m)
and allow selection of subROIs.

The user may need to edit the following lines: 34 (ROI size)

Inputs:
1. region: Input IF or RM RGB image
2. scale: scale factor
3. regName: Character array for region ('IF' or 'RM') for naming
4. path: file path

Outputs:
1. pos: Matrix of subROI positions for referencing/rerunning if needed
2. Saved images of each subROI for the region.


Original publication for citation: 
Kant, R. J., Bare, C. F., and Coulombe, K. L. K. "Tissues with 
patterned vessels or protein release induce vascular chemotaxis
in an in vitro platform", Tissue Eng. Part A, 2021.
doi:10.1089/ten.TEA.2020.0269

%}
function [pos] = getROIs (region, scale, regName, path)
%% Define subROIs

% Set predefined ROI positions based on input image
ROIside = round(375 * scale); % Analyze 375 x 375 um subROIs converted based on input scale, change as needed
ROIpos = [100, 100, ROIside, ROIside; 
    100, 110, ROIside, ROIside;
    100, 120, ROIside, ROIside;
    100, 130, ROIside, ROIside;
    100, 140, ROIside, ROIside;
    100, 150, ROIside, ROIside;]; % Default positions of subROIs
 
% Show region image and select subROI positions
imshow(region);
title('Select 4 subROIs (R-Y-G-C-B-M) for analysis and press enter to continue');

% Call on rectangular ROIs to define location
rect1 = images.roi.Rectangle(gca,'Position',ROIpos(1,:),'StripeColor','r', 'InteractionsAllowed', 'translate');
rect2 = images.roi.Rectangle(gca,'Position',ROIpos(2,:),'StripeColor','y', 'InteractionsAllowed', 'translate');
rect3 = images.roi.Rectangle(gca,'Position',ROIpos(3,:),'StripeColor','g', 'InteractionsAllowed', 'translate');
rect4 = images.roi.Rectangle(gca,'Position',ROIpos(4,:),'StripeColor','c', 'InteractionsAllowed', 'translate');
rect5 = images.roi.Rectangle(gca,'Position',ROIpos(5,:),'StripeColor','b', 'InteractionsAllowed', 'translate');
rect6 = images.roi.Rectangle(gca,'Position',ROIpos(6,:),'StripeColor','m', 'InteractionsAllowed', 'translate');
pause;

% Save subROI positions
pos1=rect1.Position();
pos2=rect2.Position();
pos3=rect3.Position();
pos4=rect4.Position();
pos5=rect5.Position();
pos6=rect6.Position();
pos = [pos1; pos2; pos3; pos4; pos5; pos6;];
saveas(gcf,fullfile(path, strcat(regName,'ROIs.tif')),'tiff'); % Save positions into array

% Crop subROIs based on position coordinates
subR1 = imcrop(region,'Position', pos1);
subR2 = imcrop(region,'Position', pos2);
subR3 = imcrop(region,'Position', pos3);
subR4 = imcrop(region,'Position', pos4);
subR5 = imcrop(region,'Position', pos5);
subR6 = imcrop(region,'Position', pos6);


% Save subROI images
iptsetpref('ImshowBorder','tight');
imshow(subR1);
saveas(gcf, fullfile(path, strcat(regName, '_ROI1.tif')), 'tiff');
imshow(subR2);
saveas(gcf, fullfile(path, strcat(regName, '_ROI2.tif')), 'tiff');
imshow(subR3);
saveas(gcf, fullfile(path, strcat(regName, '_ROI3.tif')), 'tiff');
imshow(subR4);
saveas(gcf, fullfile(path, strcat(regName, '_ROI4.tif')), 'tiff');
imshow(subR5);
saveas(gcf, fullfile(path, strcat(regName, '_ROI5.tif')), 'tiff');
imshow(subR6);
saveas(gcf, fullfile(path, strcat(regName, '_ROI6.tif')), 'tiff');

close all;
end % End of getROI.m
